package server;

/**
 * Created with IntelliJ IDEA.
 * User: nocturne
 * Date: 03.03.13
 * Time: 17:04
 * To change this template use File | Settings | File Templates.
 */

import logic.Controller;
import java.io.*;
import java.net.*;
import java.util.Calendar;


public class Server {

    private int port;

    private Controller controller;


    public Server(int p) {
        this.port = p;

        controller = new Controller();
    }

    public void start() throws IOException, ClassNotFoundException {
        ServerSocket ss = new ServerSocket(port);
        System.out.println("Server: started");
        while (true) {
            Socket s = ss.accept();
            new RequestThread(s).start();
            System.out.println(Calendar.getInstance().getTime().toString() + " " + "Server: new client connected is ip: " + s.getRemoteSocketAddress());
        }
    }

    private class RequestThread extends Thread {

        private Socket s;

        private RequestThread(Socket s) {
            this.s = s;
        }

        public void run() {

            try {

                ObjectOutputStream out = new ObjectOutputStream(s.getOutputStream());
                ObjectInputStream in = new ObjectInputStream(s.getInputStream());

                String[] command1 = null;

                try {
                    command1 = (String[])in.readObject();
                } catch (ClassNotFoundException e) {
                    System.out.println(e);
                }

                System.out.println("command " + command1[0]);

                String val = "";
                String json = null;
                String jsonresult = null;

                if(command1[0].equals("onLED1")) {
                   // serialWriter.setCommand("1");
                   //serialWriter.run("1");
                    controller.onLED1();
                }  else if(command1[0].equals("offLED1")) {
                    //serialWriter.setCommand("0");
                    //serialWriter.run("0");
                    controller.offLED1();
                }  else if(command1[0].equals("onLED2")) {
                    //serialWriter.setCommand("3");
                    //serialWriter.run("3");
                    controller.onLED2();
                }  else if(command1[0].equals("offLED2")) {
                    //serialWriter.setCommand("2");
                    //serialWriter.run("2");
                    controller.offLED2();
                }  else if(command1[0].equals("onDance")) {

                    try {

                            controller.offLED1();
                            Thread.sleep(100);
                            controller.offLED2();
                            Thread.sleep(100);


                            controller.onLED1();
                            Thread.sleep(200);
                            controller.onLED2();
                            Thread.sleep(200);

                            controller.offLED1();
                            Thread.sleep(100);
                            controller.offLED2();
                            Thread.sleep(100);


                    } catch (InterruptedException e) {
                        System.out.println("onDance error " + e);
                    }

                }  else if(command1[0].equals("offDance")) {
                    try{
                        controller.offLED1();
                        Thread.sleep(100);
                        controller.offLED2();
                        Thread.sleep(100);
                    } catch (InterruptedException e) {
                        System.out.println("offDance error " + e);
                    }
                }  else {
                    System.out.println("Error command on send server");
                }

                out.writeObject(json);
                in.close();
                out.close();

                //s.close();

            } catch (IOException e) {
                System.err.println("Server: Exception : " + e.toString());
            }
        }
    }
}
